package com.wwp.tbshop.activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.heima30.flowlayoutlib.FlowLayout;
import com.wwp.tbshop.R;
import com.wwp.tbshop.presenter.RecommendSearchPresenter;
import com.wwp.tbshop.util.TbApp;
import com.wwp.tbshop.util.UiUtil;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Xiaokun on 2017/6/18.
 */

public class SearchActivity extends BaseActivity {


    @Bind(R.id.iv_search_back)
    ImageView ivSearchBack;
    @Bind(R.id.et_search)
    EditText etSearch;
    @Bind(R.id.tv_search)
    TextView tvSearch;
    @Bind(R.id.iv_search_delete)
    ImageView ivSearchDelete;
    @Bind(R.id.fl_flowlayout)
    FlowLayout flFlowlayout;
    @Bind(R.id.fl_flowlayout_history)
    FlowLayout flFlowlayoutHistory;
    @Bind(R.id.ll_gone)
    LinearLayout llGone;
    @Bind(R.id.iv_delete_his)
    ImageView ivDeleteHis;
    private List<String> mSearchKeywords = new ArrayList<String>();
    private Random random = new Random();
    private String searchName;
    private RecommendSearchPresenter mPresenter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_search;
    }

    @Override
    protected void init() {
        processIntent();
        mPresenter = new RecommendSearchPresenter(this);
        initFlowlayout();
        etSearch.addTextChangedListener(textChangedListener);
        if (searchName != null) {
            etSearch.setText(searchName);
        }
    }

    private void initFlowlayout() {
        mPresenter.getKeyword();
        flFlowlayout.setPadding(10, 10, 10, 10);
        flFlowlayoutHistory.setPadding(10, 10, 10, 10);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (TbApp.searchHistory != null && TbApp.searchHistory.size() > 0) {
            llGone.setVisibility(View.VISIBLE);
            setHistoryData();
        } else {
            llGone.setVisibility(View.GONE);
        }
    }

    private void setHistoryData() {
        Iterator<String> iterator = TbApp.searchHistory.iterator();//取出元素
        flFlowlayoutHistory.removeAllViews();
        while (iterator.hasNext()) {
            final TextView textView = new TextView(UiUtil.getContext());
            textView.setText(iterator.next());
            textView.setTextSize(24);
            GradientDrawable bgNormal = new GradientDrawable();
            bgNormal.setCornerRadius(8);
            int a = 150 + random.nextInt(106);
            int r = random.nextInt(256);
            int g = random.nextInt(256);
            int b = random.nextInt(256);
            bgNormal.setColor(Color.argb(a, r, g, b));
            textView.setBackgroundDrawable(bgNormal);
            textView.setTextColor(Color.WHITE);
            textView.setGravity(Gravity.CENTER);
            textView.setPadding(4, 4, 4, 4);
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(UiUtil.getContext(), textView.getText(), Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(UiUtil.getContext(), SearchToActivity.class);
                    intent.putExtra("search_keyword", textView.getText());
                    startActivity(intent);
                }
            });
            flFlowlayoutHistory.addView(textView);
        }



        /*for (int i = 0; i < historyName.size(); i++) {
            final TextView textView = new TextView(UiUtil.getContext());
            textView.setText(historyName.get(i));
            textView.setTextSize(24);
            GradientDrawable bgNormal = new GradientDrawable();
            bgNormal.setCornerRadius(8);
            int a = 150 + random.nextInt(106);
            int r = random.nextInt(256);
            int g = random.nextInt(256);
            int b = random.nextInt(256);
            bgNormal.setColor(Color.argb(a, r, g, b));
            textView.setBackgroundDrawable(bgNormal);
            textView.setTextColor(Color.WHITE);
            textView.setGravity(Gravity.CENTER);
            textView.setPadding(4, 4, 4, 4);
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(UiUtil.getContext(), textView.getText(), Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(UiUtil.getContext(), SearchToActivity.class);
                    intent.putExtra("search_keyword", textView.getText());
                    startActivity(intent);
                }
            });
            flFlowlayoutHistory.addView(textView);
        }*/
    }

    private void processIntent() {
        Intent intent = getIntent();
        if (intent != null) {
            searchName = intent.getStringExtra("name");
        }
    }

    @OnClick({R.id.iv_search_back, R.id.tv_search, R.id.iv_search_delete, R.id.iv_delete_his})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_search_back:
                finish();
                break;
            case R.id.tv_search:
                String keyword = etSearch.getText().toString().trim();
                if (!TextUtils.isEmpty(keyword)) {
                    Intent intent = new Intent(this, SearchToActivity.class);
                    intent.putExtra("search_keyword", keyword);
                    startActivity(intent);
                    TbApp.searchHistory.add(keyword);
                } else {
                    Toast.makeText(this, "输入关键字", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.iv_search_delete:
                clearText();
                break;
            case R.id.iv_delete_his:
                clearHistory();
                break;
        }
    }

    private void clearHistory() {
        TbApp.searchHistory.clear();
        llGone.setVisibility(View.GONE);
    }

    private void clearText() {
        etSearch.setText("");
    }

    private TextWatcher textChangedListener = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (TextUtils.isEmpty(s)) {
                ivSearchDelete.setVisibility(View.GONE);
            } else {
                ivSearchDelete.setVisibility(View.VISIBLE);
            }
        }
    };

    public void setData(List<String> searchKeywords) {
        this.mSearchKeywords = searchKeywords;
        if (mSearchKeywords != null && mSearchKeywords.size() > 0) {
            for (int i = 0; i < mSearchKeywords.size(); i++) {
                final TextView textView = new TextView(UiUtil.getContext());
                textView.setText(mSearchKeywords.get(i));
                textView.setTextSize(24);
                GradientDrawable bgNormal = new GradientDrawable();
                bgNormal.setCornerRadius(8);
                int a = 150 + random.nextInt(106);
                int r = random.nextInt(256);
                int g = random.nextInt(256);
                int b = random.nextInt(256);
                bgNormal.setColor(Color.argb(a, r, g, b));
                textView.setBackgroundDrawable(bgNormal);
                textView.setTextColor(Color.WHITE);
                textView.setGravity(Gravity.CENTER);
                textView.setPadding(4, 4, 4, 4);
                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(UiUtil.getContext(), textView.getText(), Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(UiUtil.getContext(), SearchToActivity.class);
                        intent.putExtra("search_keyword", textView.getText());
                        startActivity(intent);
                        TbApp.searchHistory.add(textView.getText().toString());
                    }
                });
                flFlowlayout.addView(textView);
            }
        }
    }
}
